# toolchain build order: linux-api-headers->glibc->binutils->gcc->binutils->glibc

pkgname=binutils
pkgver=2.30
pkgrel=1
pkgdesc="A set of programs to assemble and manipulate binary and object files"
arch=('x86_64')
url="https://www.gnu.org/software/binutils/"
license=('GPL')
groups=('base-devel')
depends=('glibc>=2.26' 'zlib')
checkdepends=('dejagnu' 'bc')
options=('!libtool' '!distcc' '!ccache')
source=("https://sourceware.org/pub/binutils/releases/binutils-${pkgver}.tar.xz")
sha512sums=('e747ea20d8d79fcd21b9d9f6695059caa7189d60f19256da398e34b789fea9a133c32b192e9693b5828d27683739b0198431bf8b3e39fb3b04884cf89d9aa839')

prepare() {
  cd binutils-${pkgver}
  
  # libiberty configure tests 
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure
}

build() {
  mkdir -p binutils-build
  cd binutils-build

  ../binutils-${pkgver}/configure \
    --prefix=/usr \
    --with-lib-path=/usr/lib:/usr/local/lib \
    --with-bugurl=https://github.com/KaOSx/core/issues \
    --enable-ld=default \
    --enable-gold \
    --with-pic \
    --enable-plugins \
    --enable-threads \
    --enable-shared \
    --disable-werror \
    --disable-multilib \
    --disable-gdb

  make configure-host

  make tooldir=${pkgdir}/usr
}

check() {
  cd ${srcdir}/binutils-build

  # check log files
  make LDFLAGS="" -k check || true
}

package() {
  cd binutils-build
  make prefix=${pkgdir}/usr tooldir=${pkgdir}/usr install

  rm ${pkgdir}/usr/share/man/man1/{dlltool,nlmconv,windres,windmc}*
  rm ${pkgdir}/usr/lib/lib{bfd,opcodes}.so
}
